import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMInstallType;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.ui.PlatformUI;

@SuppressWarnings("deprecation")
public class Outside implements IJavaLaunchConfigurationConstants  {
	
	public static void startHeldenTest() throws CoreException{
		IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
		IVMInstallType installType = types[0];
		IVMInstall[] jres = installType.getVMInstalls();
		
		IVMInstall jre = jres[0];
		
		IPath path = JavaCore.getClasspathVariable("JAVA_HOME");
		if (path == null) {
			ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Error", "Unable to launch Tomcat",
				new Status(IStatus.ERROR, TomcatPlugin.getDefault().getDescriptor().getUniqueIdentifier(), 0, "Must define Java classpath variable TOMCAT_HOME to reference root of Tomcat installation.", null));
			return;
		}
		
		ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
		ILaunchConfigurationType type = manager.getLaunchConfigurationType(ID_JAVA_APPLICATION);
	
		ILaunchConfiguration[] configurations = manager.getLaunchConfigurations(type);
		for (int i = 0; i < configurations.length; i++) {
			ILaunchConfiguration configuration = configurations[i];
			if (configuration.getName().equals("Start Test")) {
				configuration.delete();
				break;
			}
		}
		ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null, "Start Test");
		
		workingCopy.setAttribute(ATTR_VM_INSTALL_NAME, jre.getName());
		workingCopy.setAttribute(ATTR_VM_INSTALL_TYPE, jre.getVMInstallType().getId());
		
		// specify main type and program arguments
		workingCopy.setAttribute(ATTR_MAIN_TYPE_NAME, "helden.Helden");
		workingCopy.setAttribute(ATTR_PROGRAM_ARGUMENTS, "");//TODO -rmi
		
		// specify classpath
		IPath bootstrapPath = new Path("D:/").append("helden.jar");
		IRuntimeClasspathEntry bootstrapEntry = JavaRuntime.newVariableRuntimeClasspathEntry(bootstrapPath);
		bootstrapEntry.setClasspathProperty(IRuntimeClasspathEntry.USER_CLASSES);
		
		IPath systemLibsPath = new Path(JavaRuntime.JRE_CONTAINER);
		IRuntimeClasspathEntry systemLibsEntry = JavaRuntime.newRuntimeContainerClasspathEntry(systemLibsPath, IRuntimeClasspathEntry.STANDARD_CLASSES);
		
		List<String> classpath = new ArrayList<String>();		
		classpath.add(bootstrapEntry.getMemento());
		classpath.add(systemLibsEntry.getMemento());
		
		workingCopy.setAttribute(ATTR_CLASSPATH, classpath);
		workingCopy.setAttribute(ATTR_DEFAULT_CLASSPATH, false);
		
		// specify System properties
//		workingCopy.setAttribute(ATTR_VM_ARGUMENTS, "-Djava.endorsed.dirs=\"..\\common\\endorsed\" -Dcatalina.base=\"..\" -Dcatalina.home=\"..\" -Djava.io.tmpdir=\"..\\temp\"");
		
		// specify working diretory
//		File workingDir = JavaCore.getClasspathVariable("TOMCAT_HOME").append("bin").toFile();
//		workingCopy.setAttribute(ATTR_WORKING_DIRECTORY, workingDir.getAbsolutePath());
		
		// save and launch
		ILaunchConfiguration configuration = workingCopy.doSave();
		DebugUITools.launch(configuration, ILaunchManager.RUN_MODE);
	}
}
